Common Lisp blocks provide a
non-local exit mechanism very similar to catch and
throw, but lexically rather than dynamically scoped.
This package actually implements block in terms of
catch; however, the lexical scoping allows the
optimizing byte-compiler to omit the costly catch
step if the body of the block does not actually
return-from the block.
The forms are evaluated as if by a
progn. However, if any of the forms execute(return-fromname), they will jump out and return directly from theblockform. Theblockreturns the result of the last form unless areturn-fromoccurs.The
block/return-frommechanism is quite similar to thecatch/throwmechanism. The main differences are that block names are unevaluated symbols, rather than forms (such as quoted symbols) which evaluate to a tag at run-time; and also that blocks are lexically scoped whereascatch/throware dynamically scoped. This means that functions called from the body of acatchcan alsothrowto thecatch, but thereturn-fromreferring to a block name must appear physically within the forms that make up the body of the block. They may not appear within other called functions, although they may appear within macro expansions orlambdas in the body. Block names andcatchnames form independent name-spaces.In true Common Lisp,
defunanddefmacrosurround the function or expander bodies with implicit blocks with the same name as the function or macro. This does not occur in Emacs Lisp, but this package providesdefun*anddefmacro*forms which do create the implicit block.The Common Lisp looping constructs defined by this package, such as
loopanddolist, also create implicit blocks just as in Common Lisp.Because they are implemented in terms of Emacs Lisp
catchandthrow, blocks have the same overhead as actualcatchconstructs (roughly two function calls). However, the optimizing byte compiler will optimize away thecatchif the block does not in fact contain anyreturnorreturn-fromcalls that jump to it. This means thatdoloops anddefun*functions which don't usereturndon't pay the overhead to support it.